#pragma once

#include <boostext/exception.hpp>




/**
 * This exception is used (not only in this project), whenever a Win32 function
 * is called, that might fail. It simply queries GetLastError() and stores it
 * so it can be retrieved later.
 */
class WinApiException : public boostext::exception
{
private:

	int m_ret;

public:

	/**
	 * Create a WinApiException.
	 * It calls GetLastError to store the code for later retrieval, so debugging
	 * becomes easy.
	 */
	WinApiException(const std::string& what)
		: boostext::exception(boostext::format("%1%: GetLastError returned %2%") % what.c_str() % GetLastError())
		, m_ret(GetLastError())
	{}

	/**
	 * Get the code returned by GetLastError.
	 */
	int ret() const
	{
		return m_ret;
	}
};
///////////////////////////////////////////////////////////////////////////////////////////////////

#define WIN_API(call) { if(call == 0) BOOSTEXT_THROW(WinApiException("Calling '"#call"'failed")); }
